<template>
  <div class="login">
    <div class="box">
      <img src="@/assets/login-logo.png" alt="" />
      <el-form ref="form" :model="form" :rules="rules" class="form">
        <el-form-item prop="mobile">
          <el-input
            v-model="form.mobile"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            show-password
            v-model="form.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            @keyup.enter.native="submit"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="submit" type="primary" class="btn">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysLogin } from '@/api/user'
export default {
  data () {
    return {
      form: {
        mobile: '',
        password: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', tirgger: 'change' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入正确的手机号',
            tirgger: 'change'
          }
        ],
        password: [
          { required: true, message: '请输入密码', tirgger: 'change' },
          { min: 6, max: 12, message: '请输入6-12个数字', tirgger: 'change' }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          const res = await sysLogin(this.form)
          if (res.success) {
            //存储token
            this.$store.commit('user/setToken', res.data)
            this.$message({
              message: '登录成功',
              type: 'success'
            })
            this.$router.push('/')
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  //水平居中
  justify-content: center;
  //垂直居中
  align-items: center;
  background-image: url('~@/assets/login.jpg');
  //100vw 整个屏幕宽度  100vh 整个屏幕高度
  height: 100vh;
  .box {
    width: 600px;
    height: 450px;
    // background-color: #fff;
    text-align: center;
    .btn {
      width: 100%;
      height: 60px;
      font-size: 20px;
      background-color: #407ffe;
    }
  }
  .form {
    margin-top: 20px;
    ::v-deep .el-input__inner {
      height: 60px;
      background-color: #d3e4ff;
    }
  }
  ::v-deep .el-form-item__error {
    color: #fff;
  }
}
</style>
